%******************************************************************
%** This program is specifically written to replicate Xtreg in   **
%** Hansford and Gomez's (2010) APSR article.                    **
%** Created by Min-Hua Huang, Jan 25, 2012                       **
%** Model: Least Squares, by Fixing at Minimum, with Constrained **
%**        Optimization                                          **
%******************************************************************
clear; clc; format shortG;
load HansfordGomez_Data.mat;
code=HansfordGomez_Data(:,2);
y=HansfordGomez_Data(:,27);
n=size(y,1); 
x0=[HansfordGomez_Data(:,26) HansfordGomez_Data(:,3) HansfordGomez_Data(:,25) HansfordGomez_Data(:,29) HansfordGomez_Data(:,11:23)];
grandmeanx0=mean(x0);
m=size(x0,2);
obs=(1:n)';
replicate=[y x0 code obs];
replicate=sortrows(replicate,19);
dmean=dmean1(replicate,19);
y2=[dmean(:,1) replicate(:,19:20)];
mean4a=[(replicate(:,1:18)-dmean) replicate(:,19:20)];
mean4a=sortrows(mean4a,20);
y=mean4a(:,1); y2=sortrows(y2,3);
y2=y2(:,1); % district mean
wfmin=ones(n,2); wf1min=ones(n,2); 
for i=1:1:2
    wfmin(:,i)=mean4a(:,i+1)-min(mean4a(:,i+1)); 
end
cr1=HansfordGomez_Data(:,10).*wfmin(:,2);
cr2=wfmin(:,1).*wfmin(:,2);
afixx=[ones(n,1) wfmin cr1 cr2 HansfordGomez_Data(:,11:23)];
ols_afix=(afixx'*afixx)\(afixx'*y);
%**** Set up initial values for constrained optimization ****
sigma=sqrt((y-afixx*ols_afix)'*(y-afixx*ols_afix)/(n-m-1));
x=afixx; 
r0=ols_afix;
runmax=101;
dmy=13;
a=0;
b=100;
tolv=10^-4;
y1=y;
frac=0.6;
%******** Executive least squares constrained optimization **********
lsb=LS1(y1,x,r0,a,b,runmax,dmy,tolv,y2,frac);
lssigma=sqrt(((y1-x*lsb(1:m+1)')'*(y1-x*lsb(1:m+1)'))/(n-m-1));
lsse=robuster(y1,afixx,[lsb(1:m+1)';lssigma],code);
lst=lsb(1:m+1)'./lsse;
hr=boundary(y2,frac); 
LSCOymax=max(x*lsb(1:m+1)'+y2);
LSCOymin=min(x*lsb(1:m+1)'+y2);
xmax=zeros(m+1,1); xmin=zeros(m+1,1);
   for i=1:1:m+1
       xmax(i)=max(x(:,i));
       xmin(i)=min(x(:,i));
   end    
xlimit=[xmax xmin];
v=vsign(lsb(1:m+1)');
LSCOci=cpar(lsb(1:m+1)',a-hr(2),b-hr(1),x,xlimit,v,dmy);
LSCObv=sum(LSCOci>tolv);
LSCOpredict=range1(lsb(1:m+1)',hr(2),hr(1),x,xlimit,v,dmy);
fprintf('\nReplication of the column 1 in Table 1, Hansford and Gomez (2010)\n')
fprintf('\nLeast Squares Estimation by Fixing at Minimum with Constrained Optimization\n')
fprintf('                                 beta      se       t             \n');
fprintf('Partisan Composition           % 7.3f  % 7.3f  % 7.3f\n',lsb(2),lsse(2),lst(2));
fprintf('Turnout                        % 7.3f  % 7.3f  % 7.3f\n',lsb(3),lsse(3),lst(3));
fprintf('Turnout*GOP Incumbent          % 7.3f  % 7.3f  % 7.3f\n',lsb(4),lsse(4),lst(4));
fprintf('Turnout*Partisan Composition   % 7.3f  % 7.3f  % 7.3f\n',lsb(5),lsse(5),lst(5));
fprintf('Yr52                           % 7.3f  % 7.3f  % 7.3f\n',lsb(6),lsse(6),lst(6));
fprintf('Yr56                           % 7.3f  % 7.3f  % 7.3f\n',lsb(7),lsse(7),lst(7));
fprintf('Yr60                           % 7.3f  % 7.3f  % 7.3f\n',lsb(8),lsse(8),lst(8));
fprintf('Yr64                           % 7.3f  % 7.3f  % 7.3f\n',lsb(9),lsse(9),lst(9));
fprintf('Yr68                           % 7.3f  % 7.3f  % 7.3f\n',lsb(10),lsse(10),lst(10));
fprintf('Yr72                           % 7.3f  % 7.3f  % 7.3f\n',lsb(11),lsse(11),lst(11));
fprintf('Yr76                           % 7.3f  % 7.3f  % 7.3f\n',lsb(12),lsse(12),lst(12));
fprintf('Yr80                           % 7.3f  % 7.3f  % 7.3f\n',lsb(13),lsse(13),lst(13));
fprintf('Yr84                           % 7.3f  % 7.3f  % 7.3f\n',lsb(14),lsse(14),lst(14));
fprintf('Yr88                           % 7.3f  % 7.3f  % 7.3f\n',lsb(15),lsse(15),lst(15));
fprintf('Yr92                           % 7.3f  % 7.3f  % 7.3f\n',lsb(16),lsse(16),lst(16));
fprintf('Yr96                           % 7.3f  % 7.3f  % 7.3f\n',lsb(17),lsse(17),lst(17));
fprintf('Yr2000                         % 7.3f  % 7.3f  % 7.3f\n',lsb(18),lsse(18),lst(18));
fprintf('Constant                       % 7.3f  % 7.3f  % 7.3f\n',lsb(1),lsse(1),lst(1));
fprintf('Sigma                          % 7.3f\n',lssigma);
fprintf('Max yhat                       % 7.3f\n',LSCOymax);
fprintf('Min yhat                       % 7.3f\n',LSCOymin);
fprintf('Theoretical Boundary Violations% 7.0f\n', LSCObv);
fprintf('N                              % 7.0f\n', n);
fprintf('\nPredicted Value by Varying        Min      Max\n');
fprintf('All Variables                  % 7.1f  % 7.1f\n',LSCOpredict(1,1),LSCOpredict(1,2));
fprintf('Constant                       % 7.1f  % 7.1f\n',LSCOpredict(2,1),LSCOpredict(2,2));
fprintf('Partisan Composition           % 7.1f  % 7.1f\n',LSCOpredict(3,1),LSCOpredict(3,2));
fprintf('Turnout                        % 7.1f  % 7.1f\n',LSCOpredict(4,1),LSCOpredict(4,2));
fprintf('Turnout*GOP Incumbent          % 7.1f  % 7.1f\n',LSCOpredict(5,1),LSCOpredict(5,2));
fprintf('Turnout*Partisan Composition   % 7.1f  % 7.1f\n',LSCOpredict(6,1),LSCOpredict(6,2));
fprintf('Yr52                           % 7.1f  % 7.1f\n',LSCOpredict(7,1),LSCOpredict(7,2));
fprintf('Yr56                           % 7.1f  % 7.1f\n',LSCOpredict(8,1),LSCOpredict(8,2));
fprintf('Yr60                           % 7.1f  % 7.1f\n',LSCOpredict(9,1),LSCOpredict(9,2));
fprintf('Yr64                           % 7.1f  % 7.1f\n',LSCOpredict(10,1),LSCOpredict(10,2));
fprintf('Yr68                           % 7.1f  % 7.1f\n',LSCOpredict(11,1),LSCOpredict(11,2));
fprintf('Yr72                           % 7.1f  % 7.1f\n',LSCOpredict(12,1),LSCOpredict(12,2));
fprintf('Yr76                           % 7.1f  % 7.1f\n',LSCOpredict(13,1),LSCOpredict(13,2));
fprintf('Yr80                           % 7.1f  % 7.1f\n',LSCOpredict(14,1),LSCOpredict(14,2));
fprintf('Yr84                           % 7.1f  % 7.1f\n',LSCOpredict(15,1),LSCOpredict(15,2));
fprintf('Yr88                           % 7.1f  % 7.1f\n',LSCOpredict(16,1),LSCOpredict(16,2));
fprintf('Yr92                           % 7.1f  % 7.1f\n',LSCOpredict(17,1),LSCOpredict(17,2));
fprintf('Yr96                           % 7.1f  % 7.1f\n',LSCOpredict(18,1),LSCOpredict(18,2));
fprintf('Yr2000                         % 7.1f  % 7.1f\n\n',LSCOpredict(19,1),LSCOpredict(19,2));
